﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using ProductionManagement.Application.Command.FeedingCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Dto;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;
using static ERPPublic.ParseAPI;

namespace ProductionManagement.Application.CommandHandler.FeedingCommandHandler
{
    public class ListFeedProductCommandHandler : IRequestHandler<ListFeedProductCommand, PageDto<ListFeedProductDto>>
    {
        IFeedingRepository fRep;
        IFeedProductRepository fpRep;

        public ListFeedProductCommandHandler(IFeedingRepository fRep,
        IFeedProductRepository fpRep)
        {
            this.fRep = fRep;
            this.fpRep = fpRep;
        }

        public async Task<PageDto<ListFeedProductDto>> Handle(ListFeedProductCommand request, CancellationToken cancellationToken)
        {
            List<Products> products = await GetProductList("", "", 1, 1000);
            List<Manpower> manpower = await GetPersonList("", null, null, null, null, 0, 0, 1, 100);
            var fReps = (from f in await fRep.GetAll() select f).ToList();
            var fpReps = (from fp in await fpRep.GetAll() select fp).ToList();
            var list = (from f in fReps
                       join fp in fpReps on f.FeNumber equals fp.FeNumber
                        join m in manpower on fp.ManpowerId equals m.ManpowerId
                        join p in products on fp.ProductId equals p.ProductId
                       where fp.IsDel==false
                       select new ListFeedProductDto
                       {
                           FeDate = f.FeDate,
                           FeedingId = f.FeedingId,
                           FeNumber = f.FeNumber,
                           FeDocumentNumber = f.FeDocumentNumber,
                           FeSourceType = f.FeSourceType,
                           FeNumTotal = f.FeNumTotal,
                           FeOutState = f.FeOutState,
                           FeedProductId=fp.FeedProductId,
                           ProductId = fp.ProductId,
                           FePNum = fp.FePNum,
                           FePDeliveryDate = fp.FePDeliveryDate,
                           MName=m.MName,
                       }).ToList();

            if (!string.IsNullOrEmpty(request.FeNumber))
            {
                list = list.Where(c => c.FeNumber.Contains(request.FeNumber)).ToList();
            }
            if (request.Fstate!=null)
            {
                list = list.Where(c => c.FeDate>= request.Fstate).ToList();
            }
            if (request.Fend != null)
            {
                list = list.Where(c => c.FeDate < request.Fend).ToList();
            }
            if (request.FeOutState != null)
            {
                list = list.Where(c => c.FeOutState == request.FeOutState).ToList();
            }

            PageDto<ListFeedProductDto> page = new PageDto<ListFeedProductDto>();
            page.lcount = list.Count();
            page.pcount = (int)Math.Ceiling(page.lcount * 1.0 / request.Psize);
            page.list = list.OrderByDescending(x => x.FeedProductId).Skip((request.Pindex - 1) * request.Psize).Take(request.Psize).ToList();
            return page;

        }


        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductList(string? PName, string? PNumber, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/erpcp/api/Product/ProductShow?PName=" + PName + "&PNumber=" + PNumber + "&ProductTypeId=" + 0 + "&PageIndex=" + pindex + "&PageSize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }


        /// <summary>
        /// 显示人员档案
        /// </summary>
        /// <param name="MName"></param>
        /// <param name="Mstart"></param>
        /// <param name="Mend"></param>
        /// <param name="Bstart"></param>
        /// <param name="Bend"></param>
        /// <param name="SectorId"></param>
        /// <param name="PostId"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetPersonList(string? MName, DateTime? Mstart, DateTime? Mend, DateTime? Bstart, DateTime? Bend, int? SectorId, int? PostId, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/vendor/api/Manpower/GetManpowerList?MName=" + MName + "&Mstart=" + Mstart + "&Mend=" + Mend + "&Bstart=" + Bstart + "&SectorId=&PostId=&Bend=" + Bend + "&Pindex=" + pindex + "&Psize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Manpower>>(postReponse);
            return json.data.list;
        }


        //产品
        public class Products
        {
            /// <summary>
            /// 产品Id
            /// </summary>
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int ProductId { get; set; }
            /// <summary>
            /// 标识
            /// </summary>

            public int? PCharacteristic { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PNumber { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PName { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PSpec { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PUnit { get; set; }

            /// <summary>
            /// 产品分类ID
            /// </summary>

            public int ProductTypeId { get; set; }




        }


        /// <summary>
        /// 人员档案
        /// </summary>
        public class Manpower
        {
            /// <summary>
            /// 人员档案id
            /// </summary>
            public int ManpowerId { get; set; }
            /// <summary>
            /// 档案编号
            /// </summary>
            public string? MNumber { get; set; }
            /// <summary>
            /// 登录账号
            /// </summary>
            public string? Users { get; set; }
            /// <summary>
            /// 真实姓名
            /// </summary>
            public string? MName { get; set; }
            /// <summary>
            /// 性别
            /// </summary>
            public bool? MSex { get; set; }
            /// <summary>
            /// 出生日期
            /// </summary>
            public DateTime? MBirthDay { get; set; }
            /// <summary>
            /// 身份证号码
            /// </summary>
            public string? MIdCard { get; set; }
            /// <summary>
            /// 联系电话
            /// </summary>
            public string? MPhone { get; set; }
            /// <summary>
            /// 电子邮箱
            /// </summary>
            public string? MEmail { get; set; }
            /// <summary>
            /// 微信号
            /// </summary>
            public string? MWeChat { get; set; }
            /// <summary>
            /// QQ号
            /// </summary>
            public string? MQQ { get; set; }
            /// <summary>
            /// 紧急联系人
            /// </summary>
            public string? MContact { get; set; }
            /// <summary>
            /// 紧急联系人电话
            /// </summary>
            public string? MCPhone { get; set; }
            /// <summary>
            /// 户口地址
            /// </summary>
            public string? MPermanent { get; set; }
            /// <summary>
            /// 家庭住址
            /// </summary>
            public string? MResidence { get; set; }
            /// <summary>
            /// 证件照
            /// </summary>
            public string? MIdPhoto { get; set; }
            /// <summary>
            /// 入职时间
            /// </summary>
            public DateTime? MEntryDate { get; set; }
            /// <summary>
            /// 所在部门
            /// </summary>
            public int? SectorId { get; set; }
            /// <summary>
            /// 所在岗位
            /// </summary>
            public int? PostId { get; set; }

        }
    }
}
